package org.creavi.engine.resource;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;

public class ResourceLocator {
	private static String BASE_DIRECTORY = null;

	private static String MODEL_DIRECTORY = null;

	private static String TEXTURE_DIRECTORY = null;

	private static String EFFECT_DIRECTORY = null;

	public static final String TEXT_MODEL_EXTENSION = ".txt";

	public static final String BINARY_MODEL_EXTENSION = ".bfm";

	public static final String TEXTURE_EXTENSION = ".png";

	public static final String BINARY_PARTICLE_EXTENSION = ".jme";

	public static final String TEXT_HUD_EXTENSION = ".txt";

	public static final String BINARY_HUD_EXTENSION = ".bfh";

	public static URL locateTextModel(String name) {
		return locateFile(MODEL_DIRECTORY, name + TEXT_MODEL_EXTENSION);
	}

	public static URL locateBinaryModel(String name) {
		return locateFile(MODEL_DIRECTORY, name + BINARY_MODEL_EXTENSION);
	}

	public static URL locatePngTexture(String name) {
		return locateFile(TEXTURE_DIRECTORY, name + TEXTURE_EXTENSION);
	}

	public static URL locateBinaryParticle(String name) {
		return locateFile(EFFECT_DIRECTORY, name + BINARY_PARTICLE_EXTENSION);
	}

	public static File replaceExtension(URL url, String extension)
			throws MalformedURLException, URISyntaxException {
		String file = url.toString();
		int idx = file.lastIndexOf(".");
		file = file.substring(0, idx) + extension;
		URL urf = new URL(file);
		return new File(urf.toURI());
	}

	public static URL locateFile(String name) {
		if (BASE_DIRECTORY == null) {
			return ResourceLocator.class.getClassLoader().getResource(name);
		}
		return ResourceLocator.class.getClassLoader().getResource(
				BASE_DIRECTORY + "/" + name);
	}

	public static URL locateFile(String folder, String name) {
		if (folder == null) {
			return locateFile(name);
		}
		return locateFile(folder + "/" + name);
	}

	public static URL locateFile(String folder, String name, String extension) {
		return locateFile(folder, name + extension);
	}

	public static String getBaseDirectory() {
		return BASE_DIRECTORY;
	}

	public static void setBaseDirectory(String directory) {
		BASE_DIRECTORY = directory;
	}

	public static String getModelDirectory() {
		return MODEL_DIRECTORY;
	}

	public static void setModelDirectory(String directory) {
		MODEL_DIRECTORY = directory;
	}

	public static String getTextureDirectory() {
		return TEXTURE_DIRECTORY;
	}

	public static void setTextureDirectory(String directory) {
		TEXTURE_DIRECTORY = directory;
	}

	public static String getEffectDirectory() {
		return EFFECT_DIRECTORY;
	}

	public static void setEffectDirectory(String directory) {
		EFFECT_DIRECTORY = directory;
	}
}
